package com.restTemplate;

import lombok.SneakyThrows;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author CKC
 * @date 2023/2/11 20:33
 */
public class ChatGPTTest {

    public static HttpHeaders headers = new HttpHeaders();
    private static RestTemplate restTemplate = new RestTemplate();

    static {
        headers.add("authority", "api.openai.com");
        headers.add("method", "POST");
        headers.add("path", "/v1/engines/text-davinci-003/completions");
        headers.add("scheme", "https");
        headers.add("accept", "*/*");
        headers.add("accept-encoding", "gzip, deflate, br");
        headers.add("accept-language", "zh,zh-CN;q=0.9");
        headers.add("authorization", "Bearer sess-6rK19Nw8CsoMxoua1gFgVPfqhO7Smjhim4yCECx1");
        headers.add("cache-control", "no-cache");
        headers.add("content-type", "application/json");
        headers.add("pragma", "no-cache");
        headers.add("referer", "https://platform.openai.com/");
        headers.add("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"");
        headers.add("sec-ch-ua-mobile", "?0");
        headers.add("sec-ch-ua-platform", "\"Windows\"");
        headers.add("sec-fetch-dest", "empty");
        headers.add("sec-fetch-mode", "cors");
        headers.add("sec-fetch-site", "same-site");
        headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
    }

    @SneakyThrows
    public static void main(String[] args) {

        String text = "关于python的工作纪要以及实习计划";
        getAitext(text);
    }

    private static void getAitext(String question) {
        HttpEntity<String> stringHttpEntity = new HttpEntity<>("{\"prompt\": \"" + question + "\",\"temperature\": 0,\"max_tokens\": 1024}", headers);
        ResponseEntity<String> exchange = restTemplate.postForEntity("https://api.openai.com/v1/engines/text-davinci-003/completions", stringHttpEntity, String.class);
        System.out.println(exchange.getBody());
    }



}
